package com.zhang.log4j2desensitization.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.zhang.log4j2desensitization.model.NewUser;
import com.zhang.log4j2desensitization.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class TestController {


    @GetMapping("/hello")
    private String hello() throws JsonProcessingException {
        User userInfo = new User();
        userInfo.setId(120);
        userInfo.setUserName("张晓明");
        userInfo.setPhoneNumber("13120897028");
        userInfo.setAddress("上海市浦东新区周浦镇文化活动中心");

        NewUser newUser = new NewUser();
        newUser.setUserName("张晓明");
        newUser.setPhoneNumber("13120897028");
        newUser.setAddress("上海市浦东新区周浦镇文化活动中心");

        userInfo.setNewUser(newUser);

        log.info("用户信息{}", userInfo);
        Map<String, String> map = new HashMap<>();
        map.put("userName", "张晓明");
        map.put("phoneNumber", "13120897028");
        map.put("address", "上海市浦东新区周浦镇文化活动中心");
        log.info("用户信息{}", map);
        ObjectMapper mapper = new ObjectMapper();

        ObjectNode rootNode = mapper.createObjectNode();

        ObjectNode childNode1 = mapper.createObjectNode();

        childNode1.put("userName","张晓明");

        childNode1.put("phoneNumber","13120897028");

        rootNode.set("obj1",childNode1);

        ObjectNode childNode2 = mapper.createObjectNode();

        childNode2.put("name3","val3");

        childNode2.put("name4","val4");

        rootNode.set("obj2",childNode2);

        ObjectNode childNode3 = mapper.createObjectNode();

        childNode3.put("address","上海市浦东新区周浦镇文化活动中心");

        childNode3.put("name6","val6");

        ArrayNode arrayNode = mapper.createArrayNode();
        arrayNode.add(childNode1);
        arrayNode.add(childNode2);
        arrayNode.add(childNode3);

        rootNode.set("obj3",childNode3);
        rootNode.set("obj4",arrayNode);

        String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(rootNode);
        log.info("用户信息{}", jsonString);


        return "success";
    }

}
